<?php
namespace Home\Controller;
class BgmController extends CommonController {
	public function _initialize()
	{
		$this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
		$this->Is_power("bgm");        //检测用户权限,如果错误直接返回错误,终止程序
		$this->assign('heardadminData',session('heardadminData'));
	}
	// 背景音列表
	/*public function bgmlist()
	{
		$Bgm = D('Bgm');
		$data = $Bgm->GetList();
		foreach ($data as &$vo) {
			$vo['time'] = date('Y-m-d H:i:s',$vo['time']);
			$vo['typename'] = $vo['type'] == 1 ? '<span style="color:green">显示</span>' : '<span style="color:red">隐藏</span>';
		}
		$Page = new \Think\Page(count($data),10);
				$this->page = $Page->show();
				$this->data = array_slice($data,$Page->firstRow,$Page->listRows);
		$this->display();
	}*/
	public function bgmlist()
	{
		$type = I('get.type',0,'intval');
		$db_bgm = M('Bgm');
		$db_bgmclass = M('bgmclass');
		if ($type == 0) {
			$type = $db_bgmclass->order("sortid asc")->find()['id'];
		}
		$Bgm = D('Bgm');
		$bgmlist = $Bgm->GetList($type);
		foreach ($bgmlist as &$vo) {
			$vo['time'] = date('Y-m-d H:i:s',$vo['time']);
			$vo['typename'] = $vo['type'] == 1 ? '<span style="color:green">显示</span>' : '<span style="color:red">隐藏</span>';
		}
		$this->bgmlist = $bgmlist;
		$bgmclass = $db_bgmclass->order("sortid asc")->select();
		$this->assign('bgmclass',$bgmclass);
		$this->type = $type;
		$this->display();
	}
public function bgm_ajax(){
		// p($_POST);exit();
		$id = I('post.id');
		$order = I('post.order');
		$cid = I('post.cid');
		$db_bgm = M("bgm");
		$catelist = $db_bgm->order('sortid asc')->where("cid=$cid")->select();
		$o_arr = explode(",", $id);
		if (count($catelist) == count($o_arr)) {
			foreach ($o_arr as $key => $value) {
				$data['sortid'] = $key+1;
				$db_bgm->where(array('id'=>$value))->save($data);
			}
			exit("100");
		}else{
			exit("001");
		}

	}
	// 隐藏显示背景音
	public function ShowHideBgm()
	{
		$type = I('get.type');
		$id = I('get.id');
		$data['type'] = $type == 1 ? 0 : 1;
		$Bgm = D('Bgm');
		$result = $Bgm->UodateType($id,$data);
		if ($result) {
					$this->success('修改成功');
			} else {
				$this->error('修改失败');
			}
	}

	// 分类列表
	public function classlist()
	{
		$Bgm = D('Bgm');
		$data = $Bgm->GetClass();
		$this->data = $data;
		$this->display();
	}
public function class_ajax(){
		// p($_POST);exit();
		$id = I('post.id');
		$order = I('post.order');
		$db_bgmclass = M("bgmclass");
		$catelist = $db_bgmclass->order('sortid asc')->select();
		$o_arr = explode(",", $id);
		if (count($catelist) == count($o_arr)) {
			foreach ($o_arr as $key => $value) {
				$data['sortid'] = $key+1;
				$db_bgmclass->where(array('id'=>$value))->save($data);
			}
			exit("100");
		}else{
			exit("001");
		}

	}
	// 添加分类
	public function classadd()
	{
		$this->display();
	}

	// 添加分类提交
	public function classaddpost()
	{
		$Bgm = D('Bgm');
		$name = I('post.name');
		$result = $Bgm->classaddpost($name);
		if ($result) {
			$Bgm->save(array("id"=>$result,"sortid"=>$result));
			$this->adminlog("添加分类".$result);
			$this->success('新增成功');
		} else {
			$this->error('新增失败');
		}
	}

	// 删除分类
	public function delclass()
	{
		$id = I('get.id');
		if(!empty($id)){
			if(M('bgm')->where(array('cid'=>$id))->find()!=false){
				$this->error('删除失败 该分类下有背景音', '/Home/Bgm/classlist');
			}
			$result = M('bgmclass')->where(array('id'=>$id))->delete();
		}
		$this->adminlog("删除分类".$id);
		if ($result) {
			$this->success('删除成功', '/Home/Bgm/classlist');
		} else {
			$this->error('删除成功', '/Home/Bgm/classlist');
		}
	}

	// 修改分类
	public function classedit()
	{
		$Bgm = D('Bgm');
		$id = I('get.id');
		$data = $Bgm->classedit($id);
		$this->data = $data;
		$this->display();
	}

	// 修改分类提交
	public function classeditpost()
	{
		$Bgm = D('Bgm');
		$name = I('post.name');
		$id = I('post.id');
		$result = $Bgm->classeditpost($id,$name);
			if ($result) {
		$this->adminlog("修改分类".$id);
					$this->success('修改成功');
			} else {
				$this->error('修改失败');
			}
	}

	// 添加背景音
	public function bgmaddlist()
	{
		$Bgm = D('Bgm');
		$class = $Bgm->GetClass();
		$this->class = $class;
		$this->display();
	}

	// 提交新增音频
	public function audioaddpost()
	{
		$Bgm = D('Bgm');
		$data['name'] = I('post.name');
		$data['audio']  = I('post.url2');
		$data['cid']    = I('post.cid');
		$data['time']   = time();
		// $data['audio']  = I('post.audiourl');
		$result = $Bgm->Addaudiopost($data);
		if ($result) {
			$Bgm->save(array("id"=>$result,"sortid"=>$result));
			$this->adminlog("添加背景音频".$result);
			$this->ApiSuccess('操作成功');
		} else {
			$this->ApiError('提交失败','999999');
		}
	}
}